/*     */ package org.poebot.hal;
/*     */ 
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.ObjectInputStream;
/*     */ import java.io.ObjectOutputStream;
///*     */ import java.io.PrintStream;
/*     */ import java.io.Serializable;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.LinkedList;
/*     */ import java.util.Random;
/*     */ import java.util.Scanner;
///*     */ import java.util.Set;
import org.apache.log4j.Logger;
/*     */ 
/*     */ public class UltraHal
/*     */   implements Serializable
/*     */ {
/*     */   private static final long serialVersionUID = 1L;
/*     */   public static final String WORD_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
/*     */   public static final String END_CHARS = ".!?";
/* 295 */   private Brain brain = new Brain();
/* 296 */   HashMap<String, String> ignoreList = new HashMap<String, String>();
/*     */ 
/* 298 */   private Random rand = new Random();
/*     */ 
/* 300 */   private static Logger logger = Logger.getLogger(UltraHal.class);
/*     */ 
/*     */   public boolean add(String sentence)
/*     */   {
/*  44 */     if (sentence == null) {
/*  45 */       return false;
/*     */     }
/*  47 */     sentence = sentence.trim();
/*  48 */     ArrayList<?> parts = sentenceToParts(sentence);
/*     */ 
/*  50 */     if (parts.size() < 4)
/*     */     {
/*  52 */       logger.debug("Rejected the following (too short): " + sentence);
/*  53 */       return false;
/*     */     }
/*  55 */     for (int i = 0; i < parts.size() - 3; i++) {
/*  56 */       Quad quad = new Quad((String)parts.get(i), (String)parts.get(i + 1), (String)parts.get(i + 2), (String)parts.get(i + 3));
/*  57 */       if (this.brain.getQuads().containsKey(quad)) {
/*  58 */         quad = (Quad)this.brain.getQuads().get(quad);
/*     */       }
/*     */       else {
/*  61 */         this.brain.getQuads().put(quad, quad);
/*     */       }
/*     */ 
/*  64 */       if (i == 0) {
/*  65 */         quad.setCanStart(true);
/*     */       }
/*     */ 
/*  68 */       if (i == parts.size() - 4) {
/*  69 */         quad.setCanEnd(true);
/*     */       }
/*     */ 
/*  72 */       for (int n = 0; n < 4; n++) {
/*  73 */         String word = (String)parts.get(i + n);
/*  74 */         if (!this.brain.getWords().containsKey(word)) {
/*  75 */           this.brain.getWords().put(word, new HashSet<Quad>(1));
/*     */         }
/*  77 */         HashSet<Quad> set = (HashSet<Quad>)this.brain.getWords().get(word);
/*  78 */         set.add(quad);
/*     */       }
/*     */ 
/*  81 */       if (i > 0) {
/*  82 */         String previousWord = (String)parts.get(i - 1);
/*  83 */         if (!this.brain.getPrevious().containsKey(quad)) {
/*  84 */           this.brain.getPrevious().put(quad, new HashSet<String>(1));
/*     */         }
/*  86 */         HashSet<String> set = (HashSet<String>)this.brain.getPrevious().get(quad);
/*  87 */         set.add(previousWord);
/*     */       }
/*     */ 
/*  90 */       if (i < parts.size() - 4) {
/*  91 */         String nextWord = (String)parts.get(i + 4);
/*  92 */         if (!this.brain.getNext().containsKey(quad)) {
/*  93 */           this.brain.getNext().put(quad, new HashSet<String>(1));
/*     */         }
/*  95 */         HashSet<String> set = (HashSet<String>)this.brain.getNext().get(quad);
/*  96 */         set.add(nextWord);
/*     */       }
/*     */     }
/*     */ 
/* 100 */     logger.debug("Trained on: " + sentence);
/* 101 */     return true;
/*     */   }
/*     */ 
/*     */   public String getSentence()
/*     */   {
/* 109 */     return getSentence(null);
/*     */   }
/*     */ 
/*     */   public String getSentence(String input)
/*     */   {
/* 117 */     LinkedList<String> parts = new LinkedList<String>();
/*     */ 
/* 119 */     ArrayList<?> inputWords = sentenceToParts(input);
/* 120 */     ArrayList<String> candidateWords = new ArrayList<String>();
/* 121 */     for (int i = 0; i < inputWords.size(); i++) {
/* 122 */       String candidateWord = (String)inputWords.get(i);
/* 123 */       System.out.println("CONSIDERING WORD: " + candidateWord);
/* 124 */       if ((candidateWord.compareTo(" ") == 0) || (this.ignoreList.containsKey(candidateWord.toUpperCase())) || (!this.brain.getWords().containsKey(candidateWord)))
/*     */         continue;
/* 126 */       candidateWords.add(candidateWord);
/* 127 */       System.out.println("ADDED WORD: " + candidateWord);
/*     */     }
/*     */     Quad[] quads;
///*     */     Quad[] quads;
/* 134 */     if (candidateWords.size() > 0)
/*     */     {
/* 136 */       String word = (String)candidateWords.get(this.rand.nextInt(candidateWords.size()));
/* 137 */       quads = (Quad[])((HashSet<?>)this.brain.getWords().get(word)).toArray(new Quad[0]);
/*     */     }
/*     */     else {
/* 140 */       quads = (Quad[])this.brain.getQuads().keySet().toArray(new Quad[0]);
/*     */     }
/*     */ 
/* 143 */     if (quads.length == 0) {
/* 144 */       return "";
/*     */     }
/*     */ 
/* 147 */     Quad middleQuad = quads[this.rand.nextInt(quads.length)];
/* 148 */     Quad quad = middleQuad;
/*     */ 
/* 150 */     for (int i = 0; i < 4; i++) {
/* 151 */       parts.add(quad.getToken(i));
/*     */     }
/*     */ 
/* 154 */     while (!quad.canEnd()) {
/* 155 */       String[] nextWords = (String[])((HashSet<?>)this.brain.getNext().get(quad)).toArray(new String[0]);
/* 156 */       String nextWord = nextWords[this.rand.nextInt(nextWords.length)];
/* 157 */       quad = (Quad)this.brain.getQuads().get(new Quad(quad.getToken(1), quad.getToken(2), quad.getToken(3), nextWord));
/* 158 */       parts.add(nextWord);
/*     */     }
/*     */ 
/* 161 */     quad = middleQuad;
/* 162 */     while (!quad.canStart()) {
/* 163 */       String[] previousWords = (String[])((HashSet<?>)this.brain.getPrevious().get(quad)).toArray(new String[0]);
/* 164 */       String previousWord = previousWords[this.rand.nextInt(previousWords.length)];
/* 165 */       quad = (Quad)this.brain.getQuads().get(new Quad(previousWord, quad.getToken(0), quad.getToken(1), quad.getToken(2)));
/* 166 */       parts.addFirst(previousWord);
/*     */     }
/*     */ 
/* 169 */     StringBuffer sentence = new StringBuffer();
/* 170 */     Iterator<String> it = parts.iterator();
/* 171 */     while (it.hasNext()) {
/* 172 */       String token = (String)it.next();
/* 173 */       sentence.append(token);
/*     */     }
/*     */ 
/* 176 */     return sentence.toString();
/*     */   }
/*     */ 
/*     */   public void saveBrain(File file)
/*     */   {
/* 181 */     ObjectOutputStream objectOut = null;
/* 182 */     FileOutputStream brainFile = null;
/*     */     try {
/* 184 */       brainFile = new FileOutputStream(file);
/*     */     } catch (FileNotFoundException e) {
/* 186 */       logger.error(e);
/*     */     }
/*     */     try {
/* 189 */       objectOut = new ObjectOutputStream(brainFile);
/*     */     } catch (IOException e) {
/* 191 */       logger.error(e);
/*     */     }
/*     */     try {
/* 194 */       objectOut.writeObject(this.brain);
/*     */     } catch (IOException e) {
/* 196 */       logger.error(e);
/*     */     }
/*     */     try {
/* 199 */       objectOut.close();
/*     */     } catch (IOException e) {
/* 201 */       logger.error(e);
/*     */     }
/*     */     try {
/* 204 */       brainFile.close();
/*     */     } catch (IOException e) {
/* 206 */       logger.error(e);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void loadBrain(File file)
/*     */   {
/* 212 */     ObjectInputStream objectIn = null;
/* 213 */     FileInputStream brainFile = null;
/*     */     try {
/* 215 */       brainFile = new FileInputStream(file);
/*     */     } catch (FileNotFoundException e) {
/* 217 */       logger.error(e);
/*     */     }
/*     */     try {
/* 220 */       objectIn = new ObjectInputStream(brainFile);
/*     */     } catch (IOException e) {
/* 222 */       logger.error(e);
/*     */     }
/*     */     try {
/* 225 */       this.brain = ((Brain)objectIn.readObject());
/*     */     } catch (Exception e) {
/* 227 */       logger.error(e);
/*     */     }
/*     */     try {
/* 230 */       objectIn.close();
/*     */     } catch (IOException e) {
/* 232 */       logger.error(e);
/*     */     }
/*     */     try {
/* 235 */       brainFile.close();
/*     */     } catch (IOException e) {
/* 237 */       logger.error(e);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void loadIgnoreList(File file)
/*     */   {
/* 243 */     Scanner scanner = null;
/*     */     try {
/* 245 */       scanner = new Scanner(file);
/*     */     } catch (FileNotFoundException e) {
/* 247 */       logger.error(e);
/* 248 */       return;
/*     */     }
/*     */     do
/*     */     {
/* 252 */       String word = scanner.nextLine().toUpperCase();
/* 253 */       word = word.trim();
/* 254 */       if (!this.ignoreList.containsKey(word))
/* 255 */         this.ignoreList.put(word, word);
/*     */     }
/* 250 */     while (scanner.hasNext());
/*     */ 
/* 257 */     scanner.close();
/*     */   }
/*     */ 
/*     */   private ArrayList<String> sentenceToParts(String sentence)
/*     */   {
/* 262 */     if (sentence == null)
/* 263 */       return new ArrayList<String>();
/* 264 */     sentence = sentence.trim();
/* 265 */     ArrayList<String> parts = new ArrayList<String>();
/* 266 */     char[] chars = sentence.toCharArray();
/* 267 */     int i = 0;
/* 268 */     boolean punctuation = false;
/* 269 */     StringBuffer buffer = new StringBuffer();
/* 270 */     while (i < chars.length) {
/* 271 */       char ch = chars[i];
/* 272 */       if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".indexOf(ch) >= 0 == punctuation) {
/* 273 */         punctuation = !punctuation;
/* 274 */         String token = buffer.toString();
/* 275 */         if (token.length() > 0)
/*     */         {
/* 277 */           parts.add(token);
/*     */         }
/* 279 */         buffer = new StringBuffer();
/*     */       }
/*     */       else
/*     */       {
/* 283 */         if ((i == 0) || (chars[(i - 1)] != ' ') || (chars[i] != ' ')) {
/* 284 */           buffer.append(ch);
/*     */         }
/* 286 */         i++;
/*     */       }
/*     */     }
/* 288 */     String lastToken = buffer.toString();
/* 289 */     if (lastToken.length() > 0) {
/* 290 */       parts.add(lastToken);
/*     */     }
/* 292 */     return parts;
/*     */   }
/*     */ }

/* Location:           C:\workspace\dognuts\dognuts\dognuts.jar
 * Qualified Name:     org.dognuts.hal.UltraHal
 * JD-Core Version:    0.6.0
 */